// CyTargetLinker RIN conversion,
//
// scripts to create RIN network files for several online databases
// can be used with the CyTargetLinker app for Cytoscape
//
// Copyright 2011-2013 Department of Bioinformatics - BiGCaT, Maastricht University
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package cytargetlinker.conversion;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

import org.bridgedb.IDMapperException;
import org.bridgedb.IDMapperStack;
import org.bridgedb.bio.BioDataSource;

import cytargetlinker.conversion.ArgsParser.AFilesAttributes;

public class Utils {
	
	public static void setUpLogger(Logger log, File logFile, boolean append) throws SecurityException, IOException {
		FileHandler fh = new FileHandler(logFile.getAbsolutePath(), append);
	    log.addHandler(fh);
	    log.setLevel(Level.ALL);
	    SimpleFormatter formatter = new SimpleFormatter();
	    fh.setFormatter(formatter);
	}
	
	public static IDMapperStack initIDMapper(AFilesAttributes args,  boolean transitive) {
		if(args.isBridgeDbFiles()) {
			List<File> mappingFiles = args.getBridgeDbFiles();
			if (mappingFiles.size() > 0) {
				BioDataSource.init();

				try {
					Class.forName("org.bridgedb.rdb.IDMapperRdb");
				} catch (ClassNotFoundException ex) {
					return null;
				}

				try {
					IDMapperStack gdb = new IDMapperStack();
					for(File file : mappingFiles) {
						if (file.exists()) {
							gdb.addIDMapper("idmapper-pgdb:" + file.getAbsolutePath());
						}
					}
					if(gdb.getSize() == 0) {
						return null;
					}
					gdb.setTransitive(transitive);
					return gdb;
				} catch (IDMapperException e) {
					return null;
				}
			}
		}
		
		
		return null;
	}
	
}
